#include "stm32f10x.h" // Device header
#include "Serial.h"
#include "OLED.h"
#include "LED.h"
#include <stdio.h>
#include <string.h>
#include <rtthread.h>

long hexToDec(char *source)
{
    long sum = 0;
    long t   = 1;
    int i, len;

    len = strlen(source);
    for (i = len - 1; i >= 0; i--) {
        sum += t * getIndexOfSigns(*(source + i));
        t *= 16;
    }

    return sum;
}


int getIndexOfSigns(char ch)
{
    if (ch >= '0' && ch <= '9') {
        return ch - '0';
    }
    if (ch >= 'A' && ch <= 'F') {
        return ch - 'A' + 10;
    }
    if (ch >= 'a' && ch <= 'f') {
        return ch - 'a' + 10;
    }
    return -1;
}
void InitEsp8266()
{

	rt_thread_mdelay(1000);
    Serial_SendString(USART2, "+++"); //
	rt_thread_mdelay(1000);
    Serial_SendString(USART2, "AT+CWMODE=1\r\n"); //
	rt_thread_mdelay(6000);
    Serial_SendString(USART2, "AT+CWJAP=\"Xiaomi 13\",\"88888888\"\r\n");
	rt_thread_delay(9);
    Serial_SendString(USART2, "AT+SAVETRANSLINK=1,\"8.130.12.85\",50004,\"TCP\"\r\n"); //
	rt_thread_mdelay(1000);
    Serial_SendString(USART2, "AT+CIPMUX=0\r\n"); //
    rt_thread_mdelay(1000);
    Serial_SendString(USART2, "AT+CIPMODE=1\r\n"); //
    rt_thread_mdelay(3000);
}
int main(void)
{
    LED_Init();
    NVIC_Config();
    USART_1_Config();
    USART_2_Config();
    InitEsp8266();
    rt_thread_mdelay(3000);
    Serial_SendString(USART2, "AT+RST\r\n");
    rt_thread_mdelay(9000);
    GPIO_ResetBits(GPIOE, GPIO_Pin_5);
    while (1) {
        if (Serial_RxFlag == 1) {
            GPIO_ResetBits(GPIOB, GPIO_Pin_5);
            // ????
            Serial_SendString(USART2, "GET /addsensordata?head=");
            Serial_SendNumber(USART2, Serial_RxPacket[0], 2);
            Serial_SendString(USART2, "&pm1=");
            Serial_SendNumber(USART2, Serial_RxPacket[10] * 256 + Serial_RxPacket[11], 3);
            Serial_SendString(USART2, "&pm2=");
            Serial_SendNumber(USART2, Serial_RxPacket[12] * 256 + Serial_RxPacket[13], 3);
            Serial_SendString(USART2, "&pm3=");
            Serial_SendNumber(USART2, Serial_RxPacket[14] * 256 + Serial_RxPacket[15], 3);
            Serial_SendString(USART2, "&pm4=");
            Serial_SendNumber(USART2, Serial_RxPacket[16] * 256 + Serial_RxPacket[17], 3);
            Serial_SendString(USART2, "&pm5=");
            Serial_SendNumber(USART2, Serial_RxPacket[18] * 256 + Serial_RxPacket[19], 3);
            Serial_SendString(USART2, "&pm6=");
            Serial_SendNumber(USART2, Serial_RxPacket[20] * 256 + Serial_RxPacket[21], 3);
            Serial_SendString(USART2, "&pm7=");
            Serial_SendNumber(USART2, Serial_RxPacket[22] * 256 + Serial_RxPacket[23], 3);
            Serial_SendString(USART2, "&temperature=");
            Serial_SendNumber(USART2, Serial_RxPacket[24] * 256 + Serial_RxPacket[25], 3);
            Serial_SendString(USART2, "&humidity=");
            Serial_SendNumber(USART2, Serial_RxPacket[26] * 256 + Serial_RxPacket[27], 3);
            Serial_SendString(USART2, "&temperature1=");
            Serial_SendNumber(USART2, (Serial_RxPacket[32] - 48) * 1000 + (Serial_RxPacket[33] - 48) * 100 + (Serial_RxPacket[34] - 48) * 10 + Serial_RxPacket[35] - 48, 4);
            Serial_SendString(USART2, "&humidity1=");
            Serial_SendNumber(USART2, (Serial_RxPacket[36] - 48) * 1000 + (Serial_RxPacket[37] - 48) * 100 + (Serial_RxPacket[38] - 48) * 10 + Serial_RxPacket[39] - 48, 4);
            Serial_SendString(USART2, "&pressure=");
            Serial_SendByte(USART2, Serial_RxPacket[40]);
            Serial_SendByte(USART2, Serial_RxPacket[41]);
            Serial_SendByte(USART2, Serial_RxPacket[42]);
            Serial_SendByte(USART2, Serial_RxPacket[43]);
            Serial_SendByte(USART2, Serial_RxPacket[44]);
            Serial_SendByte(USART2, Serial_RxPacket[45]);
            Serial_SendByte(USART2, Serial_RxPacket[46]);
            Serial_SendByte(USART2, Serial_RxPacket[47]);
            Serial_SendString(USART2, " HTTP/1.1\r\n");
            Serial_SendString(USART2, "Content-Type: application/json;charset=utf-8\r\n");
            Serial_SendString(USART2, "Host: 127.0.0.1:80\r\n");
            Serial_SendString(USART2, "Connection: Keep Alive\r\n\r\n");
            Serial_RxFlag = 0;
        } else {
            GPIO_SetBits(GPIOB, GPIO_Pin_5);
        }
        Serial_SendString(USART2, "GET /addsensordata?head=12");
        Serial_SendString(USART2, " HTTP/1.1\r\n");
        Serial_SendString(USART2, "Content-Type: application/json;charset=utf-8\r\n");
        Serial_SendString(USART2, "Host: 127.0.0.1:80\r\n");
        Serial_SendString(USART2, "Connection: Keep Alive\r\n\r\n");
        rt_thread_mdelay(500);
		
        if (Serial2_RxFlag == 1) {
            if (Serial2_RxPacket[0] == '1') {
                Serial_SendString(USART1, "@1\r\n");
            } else {
                Serial_SendString(USART1, "@2\r\n");
            }
            Serial2_RxFlag = 0;
        }
    }
}

